<template>
  <nav
    class="navbar"
    role="navigation"
    aria-label="main navigation"
    style="box-shadow: inset 0 1px 0 rgba(255,255,255,0.15), 0 1px 5px rgba(0,0,0,0.075);"
  >
    <div class="navbar-brand">
      <a class="navbar-item" href="#">
        <img src="/img/logo.svg" width="112" height="28">
      </a>

      <a
        role="button"
        class="navbar-burger burger"
        aria-label="menu"
        aria-expanded="false"
        data-target="navbarBasicExample"
        id="myburger"
      >
        <span aria-hidden="true"></span>
        <span aria-hidden="true"></span>
        <span aria-hidden="true"></span>
      </a>
    </div>

    <div id="navbarBasicExample" class="navbar-menu">
      <div class="navbar-start">
        <router-link class="navbar-item" to="/admin">首页</router-link>
        <div class="navbar-item has-dropdown is-hoverable">
          <router-link class="navbar-link" to="/admin/videos">视频</router-link>
          <div class="navbar-dropdown is-boxed">
            <router-link class="navbar-item" to="/admin/videos">列表</router-link>
            <router-link class="navbar-item" to="/admin/videos/create" exact>添加</router-link>
          </div>
        </div>
      </div>
      <div class="navbar-end">
        <div class="navbar-item has-dropdown is-hoverable">
          <a class="navbar-link">
            <i class="iconfont icon-user"></i>
            {{ user }}
          </a>

          <div class="navbar-dropdown is-right">
            <a class="navbar-item" @click.prevent="logout">
              <i class="iconfont icon-logout"></i>退出
            </a>
          </div>
        </div>
      </div>
    </div>
  </nav>
</template>

<script>
import { getCookie, setCookie, flash } from "../functions";
export default {
  data() {
    return {
      user: getCookie("user")
    };
  },
  created() {
    if (!this.user) {
      this.$router.push({ path: "/login" });
    }
  },
  mounted() {
    this.burgurHandler();
  },
  methods: {
    burgurHandler() {
      let el = document.getElementById("myburger");
      el.addEventListener("click", () => {
        // Get the target from the "data-target" attribute
        const target = el.dataset.target;
        const $target = document.getElementById(target);

        // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
        el.classList.toggle("is-active");
        $target.classList.toggle("is-active");
      });
    },
    logout() {
      setCookie("user", null);
      this.$router.push({ path: "/login" });
      flash("退出成功！");
    }
  }
};
</script>

